package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1.在菜品表中根据那么分页查询
        //1-1准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //1-2准备分装查询条件
        //StringUtils.isNotEmpty(name)判断name是否为空字符串
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //1-3执行菜品列表分页查询
        page = dishMapper.selectPage(page, wrapper);

        //2.遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3.根据菜品的分类id从分类表中查询分类信息(名称)
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //4.根据菜品id从口味表查询当前菜品口味列表
                //LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                //wrapper1.eq(DishFlavor::getDishId, dish.getId());
                //List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
            }
        }
        //返回结果
        return page;
    }

    //新增菜品
    @Override
    public void save(Dish dish) {
        //1.向dish表保存基本信息,(mybatisPlus自动返回主键)
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键：{}", dish.getId());

        //2.遍历前端传入的口味列表信息
        List<DishFlavor> flavorList = dish.getFlavors();
        //进行非空判断
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //3.为口味she'zhi 所属菜品id
                dishFlavor.setDishId(dish.getId());
                //4.保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //回显菜品信息
    @Override
    public Dish findById(Long id) {
        //1.从菜品表根据id查询
        Dish dish = dishMapper.selectById(id);

        //2.根据菜品的分类id从分类表中查询分类信息(名称)
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());

        //根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);
        return dish;
    }

    //修改菜品
    @Override
    public void update(Dish dish) {
        //1.根据菜品id直接更新菜品基本信息
        dishMapper.updateById(dish);
        //2.删除口味表中当前菜品的相关信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);
        //3.将修改之后的口味列表重新保存到口味表中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                //DishFlavor dishFlavor1 = new DishFlavor();
                //dishFlavor1.setDishId(dishFlavor.getDishId());
                //dishFlavor1.setName(dishFlavor.getName());
                //dishFlavor1.setValue(dishFlavor.getValue());
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //禁用 启用
    @Override
    public void updateByIds(Long[] ids, Integer status) {
        for (Long id : ids) {
            //update dish set status = #{} where id = #{}
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    //删除 批量删除
    @Override
    public void deleteByIds(Long[] ids) {
        //1.判断要删除的商品是否是全部停售状态
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        int count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("要删除的菜品还在在售状态，不能删除");
        }
        //2.遍历删除
        for (Long id : ids) {
            //2-1.根据菜品id删除菜品的口味列表
            LambdaQueryWrapper<DishFlavor> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper2);

            //2.根据菜品id删除菜品信息
            dishMapper.deleteById(id);
        }
    }

    //根据菜品id查询分类列表
    @Override
    public List<Dish> findList(Long categoryId, String name, Integer status) {
        LambdaQueryWrapper<Dish> wapper = new LambdaQueryWrapper<>();
        wapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        wapper.eq(Dish::getStatus, status);
        wapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //菜品集合
        List<Dish> dishList = dishMapper.selectList(wapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品的id从口味表中查询菜品口味信息
                //select * from dish_flavor where dish_id = #{dish_id}
                LambdaQueryWrapper<DishFlavor> wapper1 = new LambdaQueryWrapper<>();
                wapper1.eq(DishFlavor::getDishId, dish.getId());

                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }

}
